package com.tjs.lucky.infrastructure.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.FileNotFoundException;

/**
 * @author TJS
 * @Description
 * @create 2023/7/19 22:15
 */
@Component
public class EmailUtil {

    @Autowired
    private JavaMailSender sender;

    @Value("${spring.mail.username}")
    private String from;


    /**
     * @param to      收件方
     * @param subject 主题
     * @param content 邮件内容
     */
    public void sendmail(String to, String subject, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setTo(to);
        message.setSubject(subject);
        message.setText(content);
        message.setFrom(from);
        sender.send(message);
    }

    /**
     * @param to      收件方
     * @param subject 主题
     * @param content html字符串
     * @throws MessagingException
     */
    public void sendhtml(String to, String subject, String content) throws MessagingException {
        MimeMessage message = sender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content, true);
        sender.send(message);
    }

    /**
     * 发送带附件的邮件
     *
     * @param to        收件方
     * @param subject   主题
     * @param content   html字符串
     * @param filepaths 附件系统路径
     * @throws MessagingException
     */
    public void sendhtmlwithFile(String to, String subject, String content, String[] filepaths) throws MessagingException {
        MimeMessage message = sender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content, true);

        for (String filepath : filepaths) {
            FileSystemResource file = new FileSystemResource(new File(filepath));
            String filename = file.getFilename();
            assert filename != null;
            helper.addAttachment(filename, file);
        }
        sender.send(message);
    }

    /**
     * 发送带有图片的邮件
     *
     * @param to      收件方
     * @param subject 主题
     * @param content html字符串
     * @param images  图片的路径，可以是绝对路径，也可以是相对路径
     * @throws MessagingException
     */
    public void sendImage(String to, String subject, String content, String[][] images) throws MessagingException {
        MimeMessage message = sender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content, true);

        for (String[] image : images) {
            String path = image[0];
            String rscId = image[1];
            FileSystemResource resource = new FileSystemResource(new File(path));
            helper.addInline(rscId, resource);
        }
        sender.send(message);
    }

}
